//
//  MercManageTableVIewCell.swift
//  kxf
//
//  Created by Elavatine on 2024/10/4.
//


import Foundation
import UIKit
import MCToast

class MercManageTableVIewCell: UITableViewCell {
    
    var mercNo = ""
    var mercChannel = ""
    var phone = ""
    
    var peizhiBlock:(()->())?
    var manageSettleBlock:(()->())?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        initUI()
    }
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.layer.cornerRadius = kFitWidth(8)
        vi.clipsToBounds = true
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        return vi
    }()
    lazy var headImgView: UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "performance_cell_avatar")
        
        return img
    }()
    lazy var nameLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        
        return lab
    }()
    lazy var contentBgView: UIView = {
        let vi = UIView()
        vi.backgroundColor = WHColor_16(colorStr: "F4F4F4")
        vi.isUserInteractionEnabled = true
        vi.clipsToBounds = true
        vi.layer.cornerRadius = kFitWidth(8)
        
        return vi
    }()
    lazy var mercTypeLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var idcardNameLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var mercNoLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var mercNoChannelLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var phoneLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var timeLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var statusLabel: UILabel = {
        let lab = UILabel()
        lab.layer.borderColor = WHColor_16(colorStr: "999999").cgColor
        lab.layer.borderWidth = kFitWidth(1)
        lab.layer.cornerRadius = kFitWidth(11)
        lab.textAlignment = .center
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        lab.textColor = WHColor_16(colorStr: "999999")
        
        return lab
    }()
    lazy var copyMercNoButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("复制", for: .normal)
        btn.backgroundColor = WHColor_16(colorStr: "FFB2B2")
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(11)
        btn.clipsToBounds = true
        
        btn.addTarget(self, action: #selector(copyMercNoAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var copyMercNoChannelButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("复制", for: .normal)
        btn.backgroundColor = WHColor_16(colorStr: "FFB2B2")
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(12)
        btn.clipsToBounds = true
        
        btn.addTarget(self, action: #selector(copyMercNoChannelAction), for: .touchUpInside)
        
        
        return btn
    }()
    lazy var copyPhoneButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("复制", for: .normal)
        btn.backgroundColor = WHColor_16(colorStr: "FFB2B2")
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(12)
        btn.clipsToBounds = true
        
        btn.addTarget(self, action: #selector(copyPhoneAction), for: .touchUpInside)
        
        
        return btn
    }()
    
    lazy var deviceNumLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var settleTypeLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
}

extension MercManageTableVIewCell{
    func updateUI(dict:NSDictionary) {
        nameLabel.text = "\(dict["merchantName"]as? String ?? "")"
        timeLabel.text = "入网时间：\(dict["createDate"]as? String ?? "")"
        phoneLabel.text = "手机号：\(dict["phone"]as? String ?? "")"
        
        idcardNameLabel.text = "证件名称：\(dict["name"]as? String ?? "")"
        mercNoLabel.text = "平台商户号：\(dict["bangdingcommercode"]as? String ?? "无")"
        mercNoChannelLabel.text = "通道商户号：\(dict["mchId"]as? String ?? "无")"
        
        mercNo = "\(dict["bangdingcommercode"]as? String ?? "无")"
        mercChannel = "\(dict["mchId"]as? String ?? "无")"
        phone = "\(dict["phone"]as? String ?? "")"
        
        deviceNumLabel.text = "设备数量：\(dict["devicesNumber"]as? String ?? "0")"
        
        if dict["bangdingcommercode"]as? String ?? "" == "" {
            copyMercNoButton.isHidden = true
        }else{
            copyMercNoButton.isHidden = false
        }
        if dict["mchId"]as? String ?? "" == "" {
            copyMercNoChannelButton.isHidden = true
        }else{
            copyMercNoChannelButton.isHidden = false
        }
        
        if dict["status"]as? String ?? "" == "已完成" {
            statusLabel.text = "已入网"
            statusLabel.isUserInteractionEnabled = true
            statusLabel.layer.borderColor = UIColor.THEME.cgColor
            statusLabel.textColor = .THEME
        }else{
            statusLabel.text = dict["status"]as? String ?? ""
            statusLabel.isUserInteractionEnabled = false
            statusLabel.layer.borderColor = UIColor.COLOR_LINE_GREY.cgColor
            statusLabel.textColor = WHColor_16(colorStr: "999999")
        }
        
        
        if dict["mchType"]as? String ?? "" == "1"{
            mercTypeLabel.text = "商户类型：小微商户"
        }else if dict["mchType"]as? String ?? "" == "2"{
            mercTypeLabel.text = "商户类型：个体商户"
        }else{
            mercTypeLabel.text = "商户类型：企业商户"
        }
        
//        if dict["type"]as? Int ?? -1 == 0{
//            settleTypeLabel.text = "结算方式：D0结算"
//        }else if dict["type"]as? Int ?? -1 == 1{
//            settleTypeLabel.text = "结算方式：D1结算"
//        }else if dict["type"]as? Int ?? -1 == 2{
//            settleTypeLabel.text = "结算方式：T0结算"
//        }else if dict["type"]as? Int ?? -1 == 3{
//            settleTypeLabel.text = "结算方式：T1结算"
//        }else if dict["type"]as? Int ?? -1 == 4{
//            settleTypeLabel.text = "结算方式：定时结算"
//        }
        settleTypeLabel.text = "结算方式：\(dict["settleCycleName"]as? String ?? "无")"
    }
    
    @objc func copyMercNoAction() {
        UIPasteboard.general.string = mercNo
        MCToast.mc_text("平台商户号已复制！")
    }
    @objc func copyMercNoChannelAction() {
        UIPasteboard.general.string = mercChannel
        MCToast.mc_text("通道商户号已复制!")
    }
    @objc func copyPhoneAction() {
        UIPasteboard.general.string = phone
        MCToast.mc_text("手机号已复制！")
    }
}

extension MercManageTableVIewCell{
    func initUI() {
        contentView.addSubview(whiteView)
        whiteView.addSubview(headImgView)
        whiteView.addSubview(nameLabel)
        
        whiteView.addSubview(contentBgView)
        contentBgView.addSubview(mercTypeLabel)
        contentBgView.addSubview(statusLabel)
        contentBgView.addSubview(idcardNameLabel)
        contentBgView.addSubview(mercNoLabel)
        contentBgView.addSubview(mercNoChannelLabel)
        contentBgView.addSubview(phoneLabel)
        contentBgView.addSubview(timeLabel)
        contentBgView.addSubview(copyMercNoButton)
        contentBgView.addSubview(copyMercNoChannelButton)
        contentBgView.addSubview(copyPhoneButton)
        
        whiteView.addSubview(deviceNumLabel)
        whiteView.addSubview(settleTypeLabel)
        
        setConstrait()
    }
    func setConstrait() {
        whiteView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(1))
            make.right.equalTo(kFitWidth(-1))
            make.top.equalTo(kFitWidth(12))
            make.height.equalTo(kFitWidth(321))
        }
        headImgView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(21))
            make.top.equalTo(kFitWidth(13))
            make.width.height.equalTo(kFitWidth(22))
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImgView.snp.right).offset(kFitWidth(6))
            make.centerY.lessThanOrEqualTo(headImgView)
            make.right.equalTo(kFitWidth(-10))
        }
        contentBgView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(13))
            make.right.equalTo(kFitWidth(-13))
            make.top.equalTo(kFitWidth(50))
            make.bottom.equalTo(kFitWidth(-51))
        }
        mercTypeLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(16))
            make.top.equalTo(kFitWidth(19))
            make.right.equalTo(kFitWidth(-90))
        }
        statusLabel.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-12))
            make.centerY.lessThanOrEqualTo(mercTypeLabel)
            make.width.equalTo(kFitWidth(71))
            make.height.equalTo(kFitWidth(22))
        }
        idcardNameLabel.snp.makeConstraints { make in
            make.left.equalTo(mercTypeLabel)
            make.top.equalTo(kFitWidth(53))
            make.right.equalTo(kFitWidth(-20))
        }
        mercNoLabel.snp.makeConstraints { make in
            make.left.equalTo(mercTypeLabel)
            make.top.equalTo(kFitWidth(87))
            make.right.equalTo(kFitWidth(-70))
        }
        copyMercNoButton.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-11))
            make.width.equalTo(kFitWidth(54))
            make.height.equalTo(kFitWidth(22))
            make.centerY.lessThanOrEqualTo(mercNoLabel)
        }
        mercNoChannelLabel.snp.makeConstraints { make in
            make.left.equalTo(mercTypeLabel)
            make.top.equalTo(kFitWidth(121))
            make.right.equalTo(mercNoLabel)
        }
        copyMercNoChannelButton.snp.makeConstraints { make in
            make.right.width.height.equalTo(copyMercNoButton)
            make.centerY.lessThanOrEqualTo(mercNoChannelLabel)
        }
        phoneLabel.snp.makeConstraints { make in
            make.left.equalTo(mercTypeLabel)
            make.top.equalTo(kFitWidth(155))
        }
        copyPhoneButton.snp.makeConstraints { make in
            make.right.width.height.equalTo(copyMercNoButton)
            make.centerY.lessThanOrEqualTo(phoneLabel)
        }
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(mercTypeLabel)
            make.top.equalTo(kFitWidth(185))
        }
        
        deviceNumLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(29))
            make.bottom.equalTo(kFitWidth(-15))
        }
        settleTypeLabel.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-18))
            make.centerY.lessThanOrEqualTo(deviceNumLabel)
        }
        
    }
}
